package cn.ainit.base.myTask;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.bj.flow.entity.ReturnNodeStore;
import cn.ainit.sys.ds.store.StoreService;
import cn.ainit.sys.menu.SessionCont;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.query.entity.QueryJson;
 
@Controller
@RequestMapping("/myTask")
public class MyTaskController {
	MyTaskService myTaskService;
	
	@Resource
	public void setMyTaskService(MyTaskService myTaskService) {
		this.myTaskService = myTaskService;
	}
	private StoreService storeService;

	@Resource
	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}
	/**
	 * 点击任务 执行的方法
	 * @param taskParam
	 * @param request
	 * @param session 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findMyTask")
	@ResponseBody
	public QueryJson findMyTask(TaskParam taskParam, HttpServletRequest request,HttpSession session) throws Exception{
		QueryJson queryJson = new QueryJson();
		try {
		    Object uid = session.getAttribute(SessionCont.uid);
			Object staid = session.getAttribute(SessionCont.STATION_ID);
			String uId =uid == null?"":uid.toString();
			String staId =staid == null?"":staid.toString();
			taskParam.setUid(uId);
			taskParam.setStaId(staId);
			queryJson = this.myTaskService.findTask(taskParam);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return queryJson;
	}
	@RequestMapping("/dsIdByType")
	@ResponseBody
	public ReturnNodeStore getDsIdByNode(String djType,HttpServletRequest request) throws Exception{
		System.out.println(djType);
		ReturnNodeStore returnObject = new ReturnNodeStore();
		String flagStr = ""; //执行存储过程返回的标志位
		try {
			//获取dsId 根据 单据类型
			String dsId = this.myTaskService.getDsIdByType(djType);
			String funcId=this.myTaskService.getFuncIdByType(djType);
			returnObject.setDsId(dsId);
			String[] dsIds = dsId.split(";");
			DataStore dataStore = storeService.findStore(dsIds[0]);
			returnObject.setDataStore(dataStore);
			returnObject.setFuncId(funcId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 初始化界面时加载左侧树的list   
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findWorkNode")
	@ResponseBody
	public List findWorkNode(HttpServletRequest request,HttpSession session) throws Exception{
		List list = new ArrayList();
		try {
			Object uid = session.getAttribute(SessionCont.uid);
			list = this.myTaskService.myWorkList(uid.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 初始化界面时加载左侧树的节点的数量
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findNodeNumber")
	@ResponseBody
	public List findNodeNumber(HttpServletRequest request,HttpSession session) throws Exception{
		List list = new ArrayList();
		try {
			Object uid = session.getAttribute(SessionCont.uid);
			list = this.myTaskService.myWorkNum(uid.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
